package com.qixiucoding.buss;

import com.alibaba.fastjson.JSONObject;
import com.qixiucoding.strategy.IStrategy;
import com.qixiucoding.strategy.StrategyBeanContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;

import java.util.Map;
import java.util.Optional;

/**
 * 业务实现类
 *
 * @Author longhj
 * @Date 2021/11/09 星期二 23:10
 */
@Slf4j
public class ETL extends BussEtl {
    @Override
    JSONObject bussWayBefore(JSONObject jsonObject) {

        return jsonObject;
    }

    @Override
    JSONObject bussWayAfter(JSONObject jsonObject) {
        Optional<String> strategy = jsonObject.keySet().stream().filter(e -> e.startsWith("strategy")).findFirst();
        String strategyName = strategy.get();
        Map<String, IStrategy> strategyBean = StrategyBeanContext.getStrategyBean();
        IStrategy iStrategy = strategyBean.get(strategyName);
        if (ObjectUtils.isEmpty(iStrategy)) {
            log.error("策略名称错误或者不存在策略Bean");
            return null;
        }
        return iStrategy.dealData(jsonObject);
    }
}
